#!/usr/bin/python

import ConfigParser, os

class GKVMConfigParser:

	def __init__(self):
		"""docstring for __init__"""
		self.machines = []

		self.path = '/home/' + os.getlogin() + '/.gkvm'
		if not os.path.isdir(self.path):
			return -1

		config = ConfigParser.ConfigParser()
		config.read(self.path + '/machines.cfg')

		counter = 0
		for machine in config.sections():
			hda     = self.path + '/disks/' + config.get(machine, 'hda')
			net     = config.get(machine, 'net')
			graphic = config.get(machine, 'graphic')

			self.machines.append( {'id':counter, 'name':machine, 'disk':hda, 'network':net, 'session':graphic} )
			counter = counter + 1

	def getInfoById(self, id):
		for machine in self.machines:
			if id == machine['id']:
				print "Id: \t\t"   + str( machine['id'] )
				print "Name: \t\t" + machine['name']
				print "Disk: \t\t" + self.path + '/disks/' + machine['disk']
				print "Network: \t" + machine['network']
				print "Session: \t" + machine['session']

	def getDataById(self, id):
		return [i for i in self.machines if i['id']==id][0]

	def getDataByMachineName(self, name):
		return [i for i in self.machines if i['name']==name][0]
